﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace awsomeSyntezator.PipeLine.Oscilators
{
	class SAWwawe : Oscilator
    {
		public double step1;
		public double step2;
		public bool sign;
		public double previous;
		private double current;

        public SAWwawe(float _freq, int _rate, int strenght): base(_freq, _rate, strenght)
        {
            type = 0;
			double n1 = (Math.Asin(1.0) * Options.rate) / (2.0 * Math.PI * frequency);
			double n2 = (Math.Asin(-1.0) * Options.rate) / (2.0 * Math.PI * frequency);
        	double lenght = Math.Max(n1, n2) - Math.Min(n1, n2);
			step1 = 1.0 / (lenght * 0.85);
			step2 = 1.0 / (lenght * 0.15);
        	sign = true;
			previous = 1.0;
        }
        public override double getSample(int n, double sample)
        {
			if (previous >= 1.0 - step1)
				sign = false;
			if (previous <= -1.0+ step2)
				sign = true;
			if (sign)
				current = previous + step2;
			else
				current = previous - step1; 
 
        	previous = current;
        	return current;

        }
	}
}
